import { Controller, Post, Req, UseGuards, Body } from '@nestjs/common'
import { ApiOperation, ApiTags, ApiBearerAuth, ApiConsumes } from '@nestjs/swagger'
import { OrderService } from './order.service'
import { ListOrderDto } from './dto/list-order.dto'
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard'
import { delOrderDto } from './dto/del-order.dto'
import { CompleteOrderDto } from './dto/complete-dto'
import { orderScoreDto } from './dto/order-score.dto'
import { OrderScoreService } from './score/order-score.service'
import { ViewScoreDto } from './dto/get-score.dto'
import { RateLimit } from '../common/rateLimit/rate-limit.decorator'
@ApiTags('order')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth('JWT')
@Controller('order')
export class OrderController {
  constructor(
    private readonly orderService: OrderService,
    private readonly orderScoreService: OrderScoreService
  ) { }

  // 订单list
  @ApiOperation({ summary: '订单list' })
  @ApiConsumes('application/x-www-form-urlencoded')
  @RateLimit(30, 60 * 1000)
  @Post('list')
  async getOrderList(@Body() data: ListOrderDto, @Req() req: any,) {
    const userId = req.user.id
    return this.orderService.orderList(userId, data)
  }

  // 删除订单
  @ApiOperation({ summary: '删除订单' })
  @ApiConsumes('application/x-www-form-urlencoded')
  @RateLimit(15, 5 * 60 * 1000)
  @Post('delete/:id')
  async delOrder(@Body() data: delOrderDto, @Req() req: any) {
    const userId = req.user.id
    await this.orderService.delete(userId, data)
    return {
      isAdd: true
    }
  }

  // 完成用餐
  @ApiOperation({ summary: '完成用餐' })
  @ApiConsumes('application/x-www-form-urlencoded')
  @RateLimit(15, 5 * 60 * 1000)
  @Post('complete/:id')
  async completeOrder(@Body() dto: CompleteOrderDto, @Req() req: any) {
    const userId = req.user.id
    await this.orderService.finishMeal(dto.id, userId)
    return {
      isAdd: true
    }
  }

  // 订单评价
  @ApiOperation({ summary: '订单评价' })
  @ApiConsumes('application/x-www-form-urlencoded')
  @RateLimit(15, 5 * 60 * 1000)
  @Post('score')
  async scoreOrder(@Body() dto: orderScoreDto, @Req() req: any) {
    const userId = req.user?.id as number
    return await this.orderScoreService.createScore(dto, userId)
  }

  // 查询订单评价
  @ApiOperation({ summary: '查看订单评价' })
  @ApiConsumes('application/x-www-form-urlencoded')
  @RateLimit(30, 60 * 1000)
  @Post('viewScore/:orderId')
  async viewScore(@Body() dto: ViewScoreDto, @Req() req: any) {
    const userId = req.user?.id as number
    return this.orderScoreService.viewOrderScore(dto.orderId, userId)
  }
}